//
//  NetworkUtil.m
//  EPGChina
//
//  Created by CreeveLiu on 15/1/15.
//  Copyright (c) 2015年 sumavision. All rights reserved.
//

#import "NetworkUtil.h"

#import <AFNetworking.h>
#import <JSONModel.h>


// 获取频道列表
NSString *const getCCTVChannelsURL = @"http://serv.cbox.cntv.cn/json/zhibo/yangshipindao/ysmc/index.json";
NSString *const getProvChannelsURL = @"http://serv.cbox.cntv.cn/json/zhibo/weishipindao/wsmc/index.json";

// 获取正在播放节目
NSString *const getPlayingEPGBaseURL = @"http://api.cntv.cn/epg/nowepg";

NSString *const getEPGBaseURL = @"http://api.cntv.cn/epg/epginfo3";

@implementation NetworkUtil

+ (void)getCCTVChannelWithCompleted:(void (^)(NSArray *, NSError *))completed
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:getCCTVChannelsURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
        NSArray *arrChannels = [self didGetChannelJSON:responseObject];
        completed(arrChannels, nil);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
        completed(nil, error);
    }];
}

+ (void)getProvChannelWithCompleted:(void (^)(NSArray *, NSError *))completed
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:getProvChannelsURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
        NSArray *arrChannels = [self didGetChannelJSON:responseObject];
        completed(arrChannels, nil);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
        completed(nil, error);
    }];
}

+ (void)getPlayingEPGWithChannelId:(NSString *)channelId
                         completed:(void (^)(PlayingEPGModel *playing, NSError *error))completed
{
    NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithCapacity:0];
    [parameters setObject:@"cbox" forKey:@"serviceId"];
    [parameters setObject:channelId forKey:@"c"];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:getPlayingEPGBaseURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
        PlayingEPGModel *playingEPG = [self didGetPlayingEpgJSON:responseObject];
        completed(playingEPG, nil);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
        completed(nil, error);
        
    }];
}

+ (void)getEPGWithChannelId:(NSString *)channelId completed:(void (^)(EPGModel *, NSError *))completed
{
    NSDate *date = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyyMMdd"];
    NSString *dateString = [dateFormatter stringFromDate:date];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithCapacity:0];
    [parameters setObject:@"cbox" forKey:@"serviceId"];
    [parameters setObject:channelId forKey:@"c"];
    [parameters setObject:dateString forKey:@"d"];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:getEPGBaseURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
        EPGModel *epg = [self didGetEPGJSON:responseObject channelId:channelId];
        completed(epg, nil);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
        completed(nil, error);
    }];
}

+ (EPGModel *)didGetEPGJSON:(NSDictionary *)response channelId:(NSString *)channelId
{
    NSDictionary *result = [response objectForKey:channelId];
    NSError *error = nil;
    EPGModel *epg = [[EPGModel alloc] initWithDictionary:result error:&error];
    if (error) {
        NSLog(@"Error:%@", error);
    }
    return epg;
}

+ (NSArray *)didGetChannelJSON:(NSDictionary *)response
{
    NSArray *result = [[response objectForKey:@"data"] objectForKey:@"items"];
    NSArray *arrChannels = [ChannelModel arrayOfModelsFromDictionaries:result error:nil];
    return arrChannels;
}

+ (PlayingEPGModel *)didGetPlayingEpgJSON:(NSDictionary *)response
{
    PlayingEPGModel *playingEPG = [[PlayingEPGModel alloc] initWithDictionary:response error:nil];
    return playingEPG;
}

@end
